const router = require('koa-router')();
const getFeedSubmissionList = require('../../app/mws/feeds/GetFeedSubmissionList');
// const apiRequest = require('../common/apiRequest');

router.prefix('/api/v1/feeds');

router.get('/', function (ctx, next) {
    ctx.body = 'this is a feeds response!'
});

router.get('/submissions/listing', async (ctx, next) => {
    const sellerId = ctx.headers.sellerid;
    console.log(ctx.headers);
    const mwsAuthToken = ctx.headers.mwsauthtoken;
    try {
        // const result = await apiRequest("http://localhost:3001/json");
        let json = await getFeedSubmissionList.getFeedSubmissionList(sellerId, mwsAuthToken);
        console.log(json);
        ctx.response.status = 200;
        ctx.response.body = {
            data: json,
        };
    } catch (error) {
        ctx.response.status = 500;
        ctx.response.body = {
            data: error,
        };
    }
});

router.get('/submissions/listing/:submissionid', async (ctx, next) => {
    const sellerId = ctx.headers.sellerid;
    const mwsAuthToken = ctx.headers.mwsauthtoken;
    // const submissionId = parseInt(ctx.params.submissionid);
    const submissionId = ctx.params.submissionid;

    // const rp = await apiRequest('http://www.baidu.com');
    // console.log(submissionId);

    try {
        let json = await getFeedSubmissionList.getFeedSubmissionList(sellerId, mwsAuthToken, [submissionId]);
        console.log(json);
        ctx.response.status = 200;
        ctx.response.body = {
            data: json,
        };
    } catch (error) {
        ctx.response.status = 500;
        ctx.response.body = {
            data: error,
        };
    }
});

module.exports = router;
